import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 单位拼接测试类
 * 验证 chartTypeList 中 value 字段正确拼接单位的效果
 */
public class UnitConcatenationTest {
    
    public static void main(String[] args) {
        System.out.println("=== chartTypeList value 单位拼接测试 ===");
        
        // 测试重量类型单位拼接
        testWeightUnitConcatenation();
        
        // 测试计数类型单位拼接
        testCountUnitConcatenation();
        
        // 测试完整的 chartTypeList 示例
        testCompleteChartTypeList();
    }
    
    /**
     * 测试重量类型单位拼接
     */
    private static void testWeightUnitConcatenation() {
        System.out.println("\n--- 重量类型单位拼接测试 ---");
        
        // 小于1000吨的情况 - 拼接"吨"
        testWeightFormat("0.5", "0.50吨");
        testWeightFormat("1.234", "1.23吨");
        testWeightFormat("10.567", "10.57吨");
        testWeightFormat("100.89", "100.89吨");
        testWeightFormat("672.669", "672.67吨");
        testWeightFormat("999.99", "999.99吨");
        
        System.out.println();
        
        // 大于等于1000吨的情况 - 拼接"千吨"
        testWeightFormat("1000", "1.00千吨");
        testWeightFormat("1200.5", "1.20千吨");
        testWeightFormat("2345.67", "2.35千吨");
        testWeightFormat("12000", "12.00千吨");
        testWeightFormat("1234567", "1234.57千吨");
    }
    
    /**
     * 测试计数类型单位拼接
     */
    private static void testCountUnitConcatenation() {
        System.out.println("\n--- 计数类型单位拼接测试 ---");
        
        // 台数拼接
        testCountFormat("3.7", "台", "4台");
        testCountFormat("10.2", "台", "10台");
        testCountFormat("100.9", "台", "101台");
        
        // 辆数拼接
        testCountFormat("5.3", "辆", "5辆");
        testCountFormat("15.8", "辆", "16辆");
        testCountFormat("50.1", "辆", "50辆");
        
        // 个数拼接
        testCountFormat("10.4", "个", "10个");
        testCountFormat("150.6", "个", "151个");
        testCountFormat("1000.2", "个", "1000个");
        
        // 件数拼接
        testCountFormat("50.2", "件", "50件");
        testCountFormat("85.7", "件", "86件");
        testCountFormat("200.1", "件", "200件");
    }
    
    /**
     * 测试完整的 chartTypeList 示例
     */
    private static void testCompleteChartTypeList() {
        System.out.println("\n--- 完整 chartTypeList 示例 ---");
        
        System.out.println("chartTypeList: [");
        System.out.println("  {\"name\": \"纸品类\", \"value\": \"" + formatValueWithUnit(new BigDecimal("672.669"), "吨") + "\", \"unit\": \"吨\"},");
        System.out.println("  {\"name\": \"大型废料\", \"value\": \"" + formatValueWithUnit(new BigDecimal("1200.5"), "吨") + "\", \"unit\": \"千吨\"},");
        System.out.println("  {\"name\": \"旧衣类\", \"value\": \"" + formatValueWithUnit(new BigDecimal("0.686"), "吨") + "\", \"unit\": \"吨\"},");
        System.out.println("  {\"name\": \"车辆类\", \"value\": \"" + formatValueWithUnit(new BigDecimal("5.3"), "辆") + "\", \"unit\": \"辆\"},");
        System.out.println("  {\"name\": \"家电\", \"value\": \"" + formatValueWithUnit(new BigDecimal("8.7"), "台") + "\", \"unit\": \"台\"},");
        System.out.println("  {\"name\": \"锂电池类\", \"value\": \"" + formatValueWithUnit(new BigDecimal("150.4"), "个") + "\", \"unit\": \"个\"},");
        System.out.println("  {\"name\": \"服装\", \"value\": \"" + formatValueWithUnit(new BigDecimal("85.2"), "件") + "\", \"unit\": \"件\"}");
        System.out.println("]");
    }
    
    /**
     * 测试重量格式化
     */
    private static void testWeightFormat(String valueStr, String expected) {
        BigDecimal value = new BigDecimal(valueStr);
        String result = formatValueWithUnit(value, "吨");
        String status = result.equals(expected) ? "✓" : "✗";
        System.out.println(status + " " + valueStr + "吨 -> \"" + result + "\" (期望: \"" + expected + "\")");
    }
    
    /**
     * 测试计数格式化
     */
    private static void testCountFormat(String valueStr, String unit, String expected) {
        BigDecimal value = new BigDecimal(valueStr);
        String result = formatValueWithUnit(value, unit);
        String status = result.equals(expected) ? "✓" : "✗";
        System.out.println(status + " " + valueStr + unit + " -> \"" + result + "\" (期望: \"" + expected + "\")");
    }
    
    /**
     * 格式化数值并添加单位（与实际代码保持一致）
     */
    private static String formatValueWithUnit(BigDecimal value, String unit) {
        if (value == null) {
            value = BigDecimal.ZERO;
        }
        
        String unitLower = unit.toLowerCase().trim();
        switch (unitLower) {
            case "吨":
            case "t":
                // 重量类型：根据大小决定显示吨还是千吨
                BigDecimal formattedWeight = value.setScale(2, RoundingMode.HALF_UP);
                if (formattedWeight.compareTo(new BigDecimal("1000")) >= 0) {
                    BigDecimal weightInKiloTons = formattedWeight.divide(new BigDecimal("1000"), 2, RoundingMode.HALF_UP);
                    return weightInKiloTons.toString() + "千吨";
                } else {
                    return formattedWeight.toString() + "吨";
                }
            case "台":
            case "辆":
            case "个":
            case "件":
                // 计数类型：保留整数并添加单位
                BigDecimal countValue = value.setScale(0, RoundingMode.HALF_UP);
                return countValue.toString() + unit;
            default:
                // 其他类型：保留2位小数
                BigDecimal defaultValue = value.setScale(2, RoundingMode.HALF_UP);
                return defaultValue.toString();
        }
    }
}
